package hust.music.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sun.jmx.snmp.Timestamp;

import hust.music.action.base.ActionBase;
import hust.music.entity.Comment;
import hust.music.entity.Message;
import hust.music.entity.User;
import hust.music.service.MusicModService;
import hust.music.util.DynamicInfo;
import hust.music.util.Page;

@Controller
@Scope("session")
public class MusicCommentsAction extends ActionBase {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	MusicModService musicModService;
	
	private int pageNow;
	private int pageSize = 5;
	private Page page;
	private Integer musicId;
	private String flag;
	private String content;
	private Integer userId;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public Integer getMusicId() {
		return musicId;
	}
	public void setMusicId(Integer musicId) {
		this.musicId = musicId;
	}

	private List<DynamicInfo> comments = new ArrayList<DynamicInfo>();
	

	public List<DynamicInfo> getComments() {
		return comments;
	}
	public void setComments(List<DynamicInfo> comments) {
		this.comments = comments;
	}
	public int getPageNow() {
		return pageNow;
	}
	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	} 
	
	public String get(){
		
		try {
			int first = (pageNow - 1) * pageSize;
			int length = musicModService.getTotalComments(musicId);
			comments = musicModService.getComments(musicId, first, this.getPageSize());
			page = new Page(this.getPageNow(), this.getPageSize(), length);
			flag="1";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			flag=e.getMessage();
			e.printStackTrace();
		}
		
		return "get";
	}
	
	public String add(){
		try {
			Comment comment = new Comment();
			comment.setContent(content);
			comment.setTime(new Date());
			comment.setIsRead(0);
			if(session.containsKey("user")){
				musicModService.addComment(comment, musicId, (User)session.get("user"));
				flag="1";
			}else{
				flag="没有登录";
				return "add";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag=e.getMessage();
		}
		return "add";
	}
	
	public String message(){
		Message message = new Message();
		message.setContent(content);
		message.setTime(new Date());
		message.setIsRead(0);
		if(session.containsKey("user")){
			musicModService.addMessage(message, (User)session.get("user"), userId);
			flag="1";
		}else{
			flag="没有登录";
			return "message";
		}
		return "message";
	}
}
